<template>
  <div class="productOpenstep">
    <!-- 第一步区域 -->
    <div
      v-if="stepIndex === 1"
      class="flex"
      style="justify-content: center; position: relative"
    >
      <!-- 左侧展示区域 -->
      <div class="showcase-section">
        <div class="click-view-container">
          <i class="iconfont icon-fangdajing"></i>点击查看
        </div>
        <div class="devices-container">
          <!-- 笔记本电脑展示 -->
          <div class="laptop-showcase">
            <transition name="viewer-fade">
            <el-image
              ref="preview"
                class="laptop-image"
              :src="url"
              :preview-src-list="[url]"
            ></el-image>
          </transition>
            <!-- <img
              src="@/assets/template/step_back1.png"
              alt="电脑端展示"
              class="laptop-image"
            /> -->
          </div>
        </div>

        <!-- 链接和二维码 -->
        <div class="link-qrcode-section">
          <div class="desktop-link">
            电脑端链接：<a
              href="https://example.com/payment-confirm"
              target="_blank"
              class="website-link"
              >https://example.com/payment-confirm</a
            >
            <i
              class="el-icon-document-copy color-back"
              style="padding-left: 10px"
            ></i>
          </div>
          <div class="qrcode-container">
            <QrcodeGenerator
              ref="customQrcode"
              :content="customQrcodeConfig.content"
              :size="customQrcodeConfig.size"
              :foreground-color="customQrcodeConfig.foregroundColor"
              :background-color="customQrcodeConfig.backgroundColor"
              :error-correction-level="customQrcodeConfig.errorCorrectionLevel"
            />
            <!-- <img
              src="@/assets/template/qrcode_example.png"
              alt="扫码预览"
              class="qrcode-image"
            /> -->
            <div class="qrcode-text">
              扫码预览<i
                class="el-icon-download"
                style="padding-left: 5px; color: #333; font-size: 17px"
              ></i>
            </div>
          </div>
        </div>
      </div>
      <!-- 右侧配置区域 -->
      <div class="configuration-section">
        <div class="config-panel">
          <!-- 企业名称输入 -->
          <div class="input-group">
            <label class="input-label">企业名称：</label>
            <input
              type="text"
              class="company-name-input"
              placeholder="请输入企业名称"
              v-model="companyName"
            />
            <span class="advanced-edit-btn" @click="handleAdvancedEdit">
              高级编辑
            </span>
          </div>
          <!-- 主题色选择 -->
          <div class="theme-color-group">
            <ThemeColorSelector
              :customColor="customColor"
              :selected-color="selectedThemeColor"
              @color-change="handleThemeColorChange"
            >
              <template #customColor>
                <div class="custom-button-container">
                  自定义
                  <el-color-picker
                    v-model="customColor"
                    show-alpha
                    @change="handleCustomColorChange"
                  ></el-color-picker>
                </div>
              </template>
            </ThemeColorSelector>
          </div>
          <!-- 插图和图标 -->
          <div class="illustration-container">
            <img
              style="max-height: 200px"
              src="@/assets/template/edit_back.png"
              alt=""
            />
          </div>
          <!-- 下一步按钮 -->
          <div class="next-button-container">
            <button class="next-step-btn" @click="goToNextStep">
              下一步 →
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 第二步区域 - 新增内容 -->
    <div v-if="stepIndex === 2" class="second-step-container">
      <WechatAuthorization :qrcode-image="qrcodeImage" @skip="goStepskip" />
    </div>
    <!-- 第三步完成页面 - 使用新组件 -->
    <div v-if="stepIndex === 3">
      <WebsiteComplete
        :phone-preview-image="phonePreviewImage"
        :desktop-preview-image="desktopPreviewImage"
        :qrcode-image="qrcodeImage"
        @design="onDesignClick"
      />
    </div>

    <!-- 步骤条区域 -->
    <div class="steps-container">
      <div class="steps-bar">
        <div>
          <img :src="stepImages[stepIndex - 1]" alt="" />
        </div>
        <div class="steps-content">
          <div
            v-for="(step, index) in steps"
            :key="index"
            class="step-item"
            :style="index === steps.length - 1 ? { paddingRight: '50px' } : {}"
          >
            <span
              class="step-number"
              :class="{
                active:
                  index < stepIndex ||
                  (index === stepIndex - 1 && stepIndex <= steps.length),
              }"
              >{{ step.number }}</span
            >
            <span class="step-text">{{ step.text }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 引用调色板
import ColorPicker from "@/components/public/ColorPicker.vue";
// 引入二维码生成组件
import QrcodeGenerator from "@/components/public/QrcodeGenerrator.vue";
// 引入第二步区域子组件
import WechatAuthorization from "./components/WechatAuthorization.vue";
// 引入完成页面子组件
import WebsiteComplete from "./components/WebsiteComplete.vue";
// 引入主题色选择子组件
import ThemeColorSelector from "@/components/public/ThemeColorSelector/ThemeColorSelector.vue";
export default {
  name: "productOpenstep",
  components: {
    WechatAuthorization,
    WebsiteComplete,
    ColorPicker,
    QrcodeGenerator,
    ThemeColorSelector,
  },
  data() {
    return {
      productImages: [
        this.$serverImgUrl + 'step_back1.png',
      ],
      url: this.$serverImgUrl + 'step_back1.png',
      // 自定义二维码配置
      customQrcodeConfig: {
        content: "https://www.ucharts.cn/v2/#/", //
        size: 100, //二维码大小
        foregroundColor: "#333333", //二维码颜色
        backgroundColor: "#ffffff", //二维码背景颜色
        errorCorrectionLevel: "M", //二维码错误 correction level
      },
      // 自定义颜色选择器绑定值
      customColor: "",
      // 自定义预设颜色的选择器绑定值
      themeColor: "#348cf8",
      companyName: "智联科创园",
      selectedThemeColor: "#1890ff",
      stepIndex: 1,
      steps: [
        { number: "第一步", text: "修改公司名称" },
        { number: "第二步", text: "配置公众号" },
        { number: "第三步", text: "公众号配置完成" },
      ],
      stepImages: [
        require("@/assets/template/step1.png"),
        require("@/assets/template/step2.png"),
        require("@/assets/template/step3.png"),
      ],
      // 优化：使用数组存储预设颜色
      presetColors: [
        { value: "#1890ff", name: "蓝色" },
        { value: "#52c41a", name: "绿色" },
        { value: "#fa8c16", name: "橙色" },
        { value: "#f5222d", name: "红色" },
      ],
      // 自定义颜色相关
      customThemeColor: "#1890ff", // 默认与选中的主题色一致
      showCustomColorPicker: false,
      tempCustomColor: "", // 临时存储自定义颜色，用于确认/取消操作
      // 第二步区域相关数据
      qrcodeImage: require("@/assets/template/qrcode_example.png"), // 公众号授权二维码图片
      // 完成页面相关数据
      phonePreviewImage: require("@/assets/template/step_back2.png"),
      desktopPreviewImage: require("@/assets/template/step_back2.png"),
    };
  },
  methods: {
    handleCustomColorChange(color) {
      console.log("自定义颜色", color);
      this.customColor = color;
    },

    // 主题色组件回调
    handleThemeColorChange(color) {
      this.selectedThemeColor = color;
    },
    // 高级编辑按钮点击事件
    handleAdvancedEdit() {
      // 跳转到编辑页面
      this.$router.push({
        path: "/productOpenwecartAfter/admin/Edit",
        query: {
          companyName: this.companyName,
          themeColor: this.selectedThemeColor,
        },
      });
    },
    handleColorChange(color, type = false) {
      console.log("选中的颜色是:", color, type);
      if (type) {
        this.selectedThemeColor = color;
        const colorObj = {
          value: color,
          name: color, // 使用颜色值作为名称，也可以根据需求修改为更有意义的名称
        };
        // 确保presetColors数组的第五个位置（索引为4）始终是最新更新的颜色
        if (this.presetColors.length >= 5) {
          // 如果数组长度大于等于5，替换第五个元素
          this.presetColors[4] = colorObj;
        } else {
          // 如果数组长度小于5，添加到第五个位置
          // 先填充undefined到数组，确保数组长度足够
          while (this.presetColors.length < 4) {
            this.presetColors.push(undefined);
          }
          // 添加新的颜色对象到第五个位置
          this.presetColors[4] = colorObj;
        }
      }
      // 处理颜色变化的其他逻辑
    },
    // 选择主题颜色
    selectThemeColor(color) {
      this.selectedThemeColor = color;
      console.log("选择的主题色：", color);
    },

    // 打开自定义颜色选择器
    openCustomColor() {
      console.log("打开自定义颜色选择器");
      // 实际项目中可以这里调用颜色选择器组件或原生颜色选择器
    },

    // 下一步按钮点击事件 - 修改后
    goToNextStep() {
      console.log("点击下一步按钮");

      // 在新标签页打开指定链接
      this.stepIndex = 2;
      // 调用后台首页数据API
      // this.$api.admin.home
      //   .getHomeDataAPI({
      //     appid: "fcadf3fd073944faa7cf189fd7c198e4",
      //     phonenumber: "15201611813",
      //   })
      //   .then((res) => {
      //     // this.templateData = res.data.data;
      //     console.log("模版组件接收的数据--------------", res);

      //     // 处理响应
      //   });
      // 暂时注释后续打开
      // if (this.stepIndex < this.steps.length) {
      //   this.stepIndex++;
      //   console.log(`前往第${this.stepIndex}步`, {
      //     companyName: this.companyName,
      //     themeColor: this.selectedThemeColor,
      //     currentStep: this.stepIndex,
      //   });
      //   // 实际项目中可以这里进行表单验证或页面跳转
      // } else {
      //   console.log("已经是最后一步");
      //   // 实际项目中可以跳转到完成页面
      // }
    },
    // 第二步区域 - 跳过按钮点击事件
    goStepskip() {
      this.stepIndex = 3; // 点击跳过直接显示完成页面
      // 实际项目中可以这里进行表单验证或页面跳转
    },
    // 完成页面 - 设计按钮点击事件
    onDesignClick() {
      console.log("点击设计我的微官网");
      // 这里可以添加跳转到设计页面的逻辑
      this.$router.push({
        path: "/productSimpleWordbench",
        query: {
          // companyName: this.companyName,
          // themeColor: this.selectedThemeColor,
        },
      });
    },

    // 禁止浏览器返回
    handlePopState() {
      // history.pushState(null, null, document.URL);
    },

    // 打开自定义颜色选择器
    openCustomColorPicker() {
      console.log("打开自定义颜色选择器");
      // 实际项目中可以实现自定义颜色选择功能
    },
  },
  mounted() {
    // 禁止点击浏览器返回按钮
    // history.pushState(null, null, document.URL);
    // window.addEventListener("popstate", this.handlePopState);
  },
  beforeUnmount() {
    // 组件卸载时移除事件监听
    // window.removeEventListener("popstate", this.handlePopState);
  },
};
</script>

<style scoped>
.productOpenstep {
  width: 100%;
  background-image: url("@/assets/template/step_back.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  padding-bottom: 1px;
  padding-top: 50px;
  box-sizing: border-box;
}
.click-view-container {
  background-color: #fff;
  border-radius: 6px;
  width: 100px;
  height: 36px;
  line-height: 36px;
  text-align: center;
  font-size: 14px;
  color: #333;
  position: absolute;
  top: 50px;
  left: -2%;
}

/* 左侧展示区域 */
.showcase-section {
  /* 现有样式保持不变 */
  position: relative;
}

/* 其他现有样式保持不变 */
.devices-container {
  position: relative;
}

/* 笔记本电脑展示 */
.laptop-showcase {
  position: relative;
  z-index: 2;
}

.laptop-image {
  width: 600px;
  height: auto;
  border-radius: 8px;
}

/* 手机展示 */
.phone-showcase {
  position: absolute;
  top: 50%;
  left: -120px;
  transform: translateY(-50%);
  z-index: 1;
}

.phone-image {
  width: 120px;
  height: auto;
  border-radius: 12px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

/* 链接和二维码区域 */
.link-qrcode-section {
  display: flex;
  align-items: center;
  gap: 40px;
  padding-left: 57px;
  margin-top: -30px;
}

.desktop-link {
  cursor: pointer;
  font-size: 14px;
  color: #666;
  margin-top: -50px;
}

.website-link {
  color: #1890ff;
  text-decoration: none;
  margin-left: 8px;
}

.website-link:hover {
  text-decoration: underline;
}

.qrcode-container {
  display: flex;
  flex-direction: column;
  background-color: #fff;
  padding: 15px 15px;
  align-items: center;
  /* gap: 10px; */
  border-radius: 14px;
}

.qrcode-image {
  width: 80px;
  height: 80px;
}

.qrcode-text {
  font-size: 12px;
  color: #666;
}

/* 右侧配置区域 */
.configuration-section {
  margin-left: 40px;
  padding-top: 20px;
  /* flex: 1;
  display: flex;
  align-items: center;
  justify-content: center; */
}

.config-panel {
  background-color: white;
  border-radius: 20px;
  padding: 40px;
  width: 500px;
}

/* 输入组样式 */
.input-group {
  margin-bottom: 30px;
  display: flex;
  align-items: center;
}

.input-label {
  font-size: 14px;
  color: #333;
  margin-right: 10px;
  white-space: nowrap;
}

.company-name-input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.company-name-input:focus {
  outline: none;
  border-color: #40a9ff;
}

.advanced-edit-btn {
  margin-left: 45px;
  padding: 8px 16px;
  background-color: white;
  color: #1890ff;
  border: 1px solid #1890ff;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.3s;
}

.advanced-edit-btn:hover {
  background-color: #1890ff;
  color: #fff;
}

/* 主题色选择 */
.theme-color-group {
  margin-bottom: 30px;
  display: flex;
  align-items: center;
}

.color-options {
  display: flex;
  gap: 12px;
  align-items: center;
}

.color-item {
  width: 25px;
  height: 25px;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.3s;
  border: 3px solid transparent;
  position: relative;
}

.color-item:hover {
  transform: scale(1.1);
}

/* 主题色选中效果 */
.color-item.selected {
  border-color: #333;
  transform: scale(1.1);
}

.color-item.selected::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 6px;
  height: 3px;
  /* background-color: red; */
  /* border-left: 2px solid white;
  border-bottom: 2px solid white; */
  transform: translate(-50%, -50%) rotate(-45deg);
}

.custom-color-item {
  font-size: 14px;
  color: #348cf8;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.custom-color-item:hover {
  background-color: #f0f0f0;
}

/* 插图容器 */
.illustration-container {
  margin-bottom: 60px;
  height: 150px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.illustration-content {
  position: relative;
  width: 200px;
  height: 120px;
}

/* 电脑图标 */
.computer-icon {
  position: absolute;
  width: 80px;
  height: 50px;
  background-color: #e6f7ff;
  border-radius: 4px;
  top: 35px;
  left: 20px;
  z-index: 2;
}

.computer-icon::before {
  content: "";
  position: absolute;
  bottom: -8px;
  left: 15px;
  width: 50px;
  height: 8px;
  background-color: #bfbfbf;
  border-radius: 2px;
}

/* 手机图标 */
.phone-icon {
  position: absolute;
  width: 30px;
  height: 55px;
  background-color: #f6ffed;
  border-radius: 3px;
  top: 30px;
  right: 30px;
  z-index: 1;
}

.phone-icon::before {
  content: "";
  position: absolute;
  top: -5px;
  left: 50%;
  transform: translateX(-50%);
  width: 10px;
  height: 5px;
  background-color: #d9d9d9;
  border-radius: 1px;
}

/* 对勾图标 */
.check-icon {
  position: absolute;
  width: 25px;
  height: 25px;
  background-color: #f6ffed;
  border-radius: 50%;
  top: 10px;
  right: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.check-icon::before {
  content: "";
  position: absolute;
  width: 10px;
  height: 5px;
  border-left: 2px solid #52c41a;
  border-bottom: 2px solid #52c41a;
  transform: rotate(-45deg) translate(1px, 1px);
}

/* 同步图标 */
.sync-icon {
  position: absolute;
  width: 25px;
  height: 25px;
  background-color: #e6f7ff;
  border-radius: 50%;
  bottom: 20px;
  left: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sync-icon::before {
  content: "";
  position: absolute;
  width: 15px;
  height: 15px;
  border: 2px solid #1890ff;
  border-top-color: transparent;
  border-radius: 50%;
}

.sync-icon::after {
  content: "";
  position: absolute;
  width: 6px;
  height: 2px;
  background-color: #1890ff;
  border-radius: 1px;
  top: 10px;
  right: 3px;
  transform: rotate(45deg);
}

/* 云图标 */
.cloud-icon {
  position: absolute;
  width: 25px;
  height: 15px;
  background-color: #fff7e6;
  border-radius: 10px;
  bottom: 15px;
  right: 50px;
}

.cloud-icon::before {
  content: "";
  position: absolute;
  width: 15px;
  height: 15px;
  background-color: #fff7e6;
  border-radius: 50%;
  top: -7px;
  left: 5px;
}

/* 下一步按钮 */
.next-button-container {
  text-align: center;
}

.next-step-btn {
  background-color: #1890ff;
  color: white;
  border: none;
  padding: 10px 90px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
  font-weight: bold;
}

.next-step-btn:hover {
  /* background-color: #40a9ff; */
}

.next-step-btn:active {
  /* background-color: #096dd9; */
}

/* 步骤条样式 */
.steps-container {
  margin-bottom: 40px;
  display: flex;
  justify-content: center;
  padding-left: 20px;
  margin-top: 50px;
}

.steps-bar {
  width: 100%;
  max-width: 1200px;
  position: relative;
}

.steps-track {
  height: 4px;
  background-color: #e8e8e8;
  position: relative;
  margin-bottom: 10px;
}

.steps-progress {
  height: 100%;
  width: 33.33%;
  background-color: #1890ff;
  position: absolute;
  top: 0;
  left: 0;
}

.steps-content {
  display: flex;
  justify-content: space-between;
}

.step-item {
  display: flex;
  flex-direction: column;
  /* align-items: center; */
  position: relative;
}
.step-number.active {
  color: #348cf8 !important;
}
.step-number {
  color: #929191;
  font-weight: bold;
  margin-bottom: 10px;
  margin-top: 10px;
}

.step-text {
  font-weight: bold;
  font-size: 14px;
  color: #3f3e3e;
  position: relative;
}

.step-item.active .step-text {
  /* color: #1890ff; */
}

/* 进度条连接线 */
.steps-track {
  background: linear-gradient(to right, #1890ff 33.33%, #e8e8e8 33.33%);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .productOpenstep {
    flex-direction: column;
    padding: 40px 20px;
  }

  .showcase-section,
  .configuration-section {
    width: 100%;
    margin-bottom: 40px;
  }

  .laptop-image {
    width: 400px;
  }

  .config-panel {
    width: 100%;
    max-width: 500px;
  }
}

@media (max-width: 768px) {
  .laptop-image {
    width: 300px;
  }

  .phone-image {
    width: 100px;
  }

  .phone-showcase {
    left: -100px;
  }

  .link-qrcode-section {
    flex-direction: column;
    gap: 20px;
  }

  .config-panel {
    padding: 25px;
  }

  .company-name-input {
    width: 100%;
    margin-bottom: 15px;
  }

  .advanced-edit-btn {
    margin-left: 0;
    width: 100%;
  }
}
</style>
